package ar.uba.fi.taller2.cmmiassistant.frontend.reportes;

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import ar.uba.fi.taller2.cmmiassistant.modelo.cmmi.Capacidad;

@SuppressWarnings("serial")
public class ResultadoCapacidadCellRenderer extends JPanel implements
		TableCellRenderer {
	private final int BORDE = 1;
	private JLabel label = new JLabel();
	private JPanel panelDibujo = new JPanel() {
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			Graphics2D g2d = (Graphics2D) g;
			Color e = getForeground().brighter();
			Color s1 = Color.WHITE;
			Rectangle rect = getBounds();
			rect.setLocation(0, BORDE);
			rect.setSize(rect.width - BORDE, rect.height - BORDE * 2);
			GradientPaint gradient = new GradientPaint(0, 0, s1, 0,
					rect.height / 2, e, true);
			g2d.setPaint(gradient);
			g2d.fillRect(rect.x, rect.y, rect.width, rect.height);
		}
	};

	public ResultadoCapacidadCellRenderer() {
		super();
		setLayout(null);
		add(label);
		add(panelDibujo);
	}

	private static final Color[] colores = new Color[] { Color.RED,
			Color.ORANGE, Color.YELLOW, new Color(127, 255, 0), Color.GREEN };

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Capacidad capacidad = (Capacidad) value;
		// panel.setSize(new Dimension(rectangle.width, rectangle.height));
		// colores[resultadoAreaProceso.getCapacidad().getNivel()
		// panelColores.setBackground(Color.GREEN);
		int nivel = capacidad.getNivel();
		if (nivel == 0) {
			label.setText("[No Calificado]");
		} else {
			label.setText(String.valueOf(nivel));
			nivel--;
		}
		panelDibujo.setForeground(colores[nivel]);
		Rectangle rect = table.getCellRect(row, column, true);
		panelDibujo.setBounds(0, 0, rect.width, rect.height);
		label.setBounds(0, 0, rect.width, rect.height);
		return this;
	}

}